#!/bin/bash

[ -z "$set_e" ] && set -e

[ -z "$1" ] && { echo '$1 is not set';exit 2; }

go=`which go`

export GO111MODULE=off
export GOPROXY=https://goproxy.io

which protoc &> /dev/null || { echo 'please install the protoc';exit 2; }

which protoc-gen-go &> /dev/null || {
    echo "command not found: protoc-gen-go"
    echo "install protoc-gen-go ....."
    go get -u -v github.com/golang/protobuf/protoc-gen-go
    exit 2;
}

# https://www.jianshu.com/p/0d776e653906


protoName=$2
case $1 in
    'go')
        goDir="./go-client/pb/"
        if [ ! -d $goDir ];then
            mkdir -p $goDir
        fi
        # protoc -I. -I/usr/local/include --proto_path=./ --go_out=plugins=grpc:$goDir --go-grpc_out=$goDir --plugin=protoc-gen-grpc=`which protoc-gen-go` --go-grpc_opt=paths=source_relative ./$protoName.proto
        protoc -I. -I/usr/local/include --proto_path=./ --go_out=$goDir --go-grpc_out=$goDir --plugin=protoc-gen-grpc=`which protoc-gen-go`  ./$protoName.proto
    ;;
    'php')
        phpDir="./php-client/src/proto/"
        # protoc --proto_path=./   --php_out=$phpDir   --grpc_out=$phpDir   --plugin=protoc-gen-grpc=`which grpc_php_plugin`   ./$protoName.proto ./google/api/*.proto
        protoc -I. -I/usr/local/include --proto_path=./   --php_out=$phpDir --grpc_out=$phpDir --plugin=protoc-gen-grpc=`which grpc_php_plugin`   ./$protoName.proto ./google/api/*.proto
    ;;
esac


# --proto_path 为输出路径
# --php_out 为生成PHP客户端代码所在的路径
# --grpc_out 为生成PHP依赖文件所在的路径
# --plugin 为PHP的插件路径（这个路径是 grpc 项目里面使用 make grpc_php_plugin 生成的路径）
